package com.arox.springinaction.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.arox.springinaction.security.Message;
import com.arox.springinaction.security.MessageBoardService;

@Controller
@RequestMapping("/messageDelete*")
public class MessageDeleteController {

    private MessageBoardService messageBoardService;

    @Autowired
    public MessageDeleteController(final MessageBoardService messageBoardService) {
	this.messageBoardService = messageBoardService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String messageDelete(@RequestParam(required = true, value = "messageId") Long messageId, Model model) {
	Message message = messageBoardService.findByMessageId(messageId);
	messageBoardService.deleteMessage(message);
	model.addAttribute("messages", messageBoardService.listMessages());
	return "redirect:messageList";
    }
}
